using System;
using MONA.Modules.Marketing.BLL;
using THANHTUNGO.Portal.Components.Security;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditWorkItem : AddEditItemControl
    {
        public event EventHandler SaveClick;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = (Request.QueryString["ServiceId"] != null) && (Request.QueryString["ActionId"] != null);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    WorkItem wi = WorkItem.Load(this.ItemID);
                    if (wi != null)
                    {
                        ddlService.SelectedValue = wi.ServiceID.ToString();
                        ddlAction.SelectedValue = wi.ActionID.ToString();
                        txtStage.Text = wi.Stage;
                        txtLastExecute.Text = wi.LastExecutedTime.ToShortDateString();
                        txtDueDate.Text = wi.DueDate.ToShortDateString();
                        chkIsExecuted.Checked = wi.IsExecuted;
                        chkIsRepeat.Checked = wi.IsRepeat;
                        ddlAssignedTo.SelectedValue = wi.AssignedToUserID.ToString();
                        ddlPriority.SelectedValue = wi.PriorityID.ToString();
                        txtSummary.Text = wi.Summary;
                    }
                }
                else if (Request.QueryString["ServiceID"] != null)
                {
                    ddlService.SelectedValue = Request.QueryString["ServiceID"];
                }
            }
        }

        protected bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                WorkItem wi = new WorkItem();
                wi.ServiceID = Convert.ToInt32(ddlService.SelectedValue);
                wi.ActionID = Convert.ToInt32(ddlAction.SelectedValue);
                wi.Stage = txtStage.Text.Trim();
                wi.LastExecutedTime = DateTime.Now;
                wi.IsExecuted = chkIsExecuted.Checked;
                wi.IsRepeat = chkIsRepeat.Checked;
                wi.AssignedToUserID = Convert.ToInt32(ddlAssignedTo.SelectedValue);
                wi.PriorityID = Int32.Parse(ddlPriority.SelectedValue);
                wi.DueDate = DateTime.Parse(txtDueDate.Text);
                wi.Summary = txtSummary.Text.Trim();
                wi.LastUpdatedByUserID = Int32.Parse(PortalSecurity.CurrentUser.Identity.ID);
                wi.LastUpdatedDate = DateTime.Now;
                wi.CreatedByUserID = Int32.Parse(PortalSecurity.CurrentUser.Identity.ID);
                wi.CreatedDate = DateTime.Now;

                if (this.ItemID == 0)
                {
                    returnId = wi.Insert();
                }
                else
                {
                    wi.ID = this.ItemID;
                    wi.Update();
                }

                ddlService.SelectedIndex = ddlAction.SelectedIndex = 0;
                txtStage.Text = txtLastExecute.Text = string.Empty;
                chkIsExecuted.Checked = chkIsRepeat.Checked = false;
            }
            return returnId > 0;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                WorkItem.Delete(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }
            this.ReturnToReferrerPage();
        }

        protected void AddEditAction_SaveClick(object sender, EventArgs e)
        {
            ddlAction.DataBind();
        }
    }
}